package com.atl.service.impl;

import com.atl.enity.po.Promotion;
import com.atl.enity.web.dto.PromotionListDTO;
import com.atl.mapper.PromotionMapper;
import com.atl.service.IPromotionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-06-27
 */
@Service
public class PromotionServiceImpl extends ServiceImpl<PromotionMapper, Promotion> implements IPromotionService {


    /**
     *
     * 获取所有
     *
     * @param promotionListDTO
     * @return
     */
    @Override
    public Page<Promotion> getAll(PromotionListDTO promotionListDTO) {
        QueryWrapper<Promotion> queryWrapper = new QueryWrapper<>();

        String promotionName = promotionListDTO.getPromotionName();
        String targetGroup = promotionListDTO.getTargetGroup();
        String distributionMethod = promotionListDTO.getDistributionMethod();
        LocalDate validPeriodStart = promotionListDTO.getValidPeriodStart();
        LocalDate validPeriodEnd = promotionListDTO.getValidPeriodEnd();

        if (StringUtils.isNotBlank(promotionName)) {
            queryWrapper.like("promotion_name", promotionName);
        }

        if (StringUtils.isNotBlank(targetGroup)) {
            queryWrapper.eq("target_group", targetGroup);
        }

        if (StringUtils.isNotBlank(distributionMethod)) {
            queryWrapper.eq("distribution_method", distributionMethod);
        }

        if (validPeriodStart != null && validPeriodEnd != null) {
            queryWrapper.between("valid_period_start", validPeriodStart, validPeriodEnd);
            //queryWrapper.between("valid_period_end", validPeriodStart, validPeriodEnd);
        }

        return this.page(new Page<>(promotionListDTO.getCurrent(), promotionListDTO.getPageSize()), queryWrapper);
    }
}
